package com.badibadi.adapter;

import java.util.List;
import net.tsz.afinal.FinalBitmap;

import com.badibadi.activity.ClubHomepageActivity;
import com.badibadi.infos.ClubModel;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ClubOverviewZuixinAdapter_1 extends BaseAdapter {

	private Context context;
	private List<ClubModel> clubModels;
	private DisplayImageOptions options;
	private String type;
	private double map_x, map_y;

	public ClubOverviewZuixinAdapter_1(Context context,
			List<ClubModel> clubModels, String type, double map_x, double map_y) {
		this.context = context;
		this.clubModels = clubModels;
		this.type = type;
		this.map_x = map_x;
		this.map_y = map_y;
		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_stub)
				.showImageForEmptyUri(R.drawable.noimage)
				.showImageOnFail(R.drawable.noimage).cacheInMemory()
				.cacheOnDisc().displayer(new RoundedBitmapDisplayer(5)).build();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		if (clubModels.size() == 0) {
			return 0;
		}
		if (clubModels.size() == 1) {
			return 2;
		} else
			return (clubModels.size() + 1);
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		int pos = 0;
		if (position == 0) {
			pos = 0;
			return clubModels.get(pos);
		} else if (position == 1) {
			pos = 0;
			return clubModels.get(pos);
		} else if (position > 1) {
			pos = position - 2;
			return clubModels.get(pos);
		}
		return clubModels.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = LayoutInflater.from(context);
		//    
//		   
		if (position > 1) {
			final int pos = position - 1;
			convertView = inflater.inflate(
					R.layout.regionhomepage_import_layout2, null);

			convertView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
				/*	if (!Utils.getUid(context).equals("")) {*/
					
						Intent intent = new Intent(context,
								ClubHomepageActivity.class);
						if (clubModels.get(pos).getClub_id() != null) {
							intent.putExtra("cid", clubModels.get(pos)
									.getClub_id());
						} else {
							intent.putExtra("cid", clubModels.get(pos).getId());
						}
						intent.putExtra("c_uid", clubModels.get(pos).getUid());
						intent.putExtra("is_join", clubModels.get(pos)
								.getIs_joined());
						intent.putExtra("logo", clubModels.get(pos).getLogo());
//						   
//								+ clubModels.get(pos).getId() + " "
//								+ clubModels.get(pos).getUid() + " "
//								+ clubModels.get(pos).getIs_joined() + " "
//								+ clubModels.get(pos).getLogo());
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						context.startActivity(intent);
					} /*else {
						Toast.makeText(
								context,
								context.getResources()
										.getString(R.string.l_xb10), 0).show();
					}
				}*/
			});
			TextView clubOverview_item_OverAddress = (TextView) convertView
					.findViewById(R.id.clubOverview_item_OverAddress);// 发源地
			TextView clubOverview_item_name = (TextView) convertView
					.findViewById(R.id.clubOverview_item_name);// 俱乐部名称
			TextView clubOverview_item_infos = (TextView) convertView
					.findViewById(R.id.clubOverview_item_infos);// 俱乐部介绍
			TextView clubOverview_item_addnum = (TextView) convertView
					.findViewById(R.id.clubOverview_item_addnum);// 会员人数
			TextView clubOverview_item_price = (TextView) convertView
					.findViewById(R.id.clubOverview_item_price);// 加入所需金额
			TextView clubOverview_item_distance = (TextView) convertView
					.findViewById(R.id.clubOverview_item_distance);// 与我的距离
			final ImageView clubOverview_item_img = (ImageView) convertView
					.findViewById(R.id.clubOverview_item_img);// 俱乐部背景
			clubOverview_item_addnum.setText(clubModels.get(pos).getAddnum());
			clubOverview_item_infos.setText(clubModels.get(pos).getInfo());
			clubOverview_item_name.setText(clubModels.get(pos).getName());
			clubOverview_item_OverAddress.setText(clubModels.get(pos)
					.getOriginate_address());
			clubOverview_item_price.setText(clubModels.get(pos).getPrice());
			// ImageLoader.getInstance().displayImage(
			// Constants.BadiDownImgUrl
			// + clubModels.get(position).getLogo(),
			// clubOverview_item_img, options);
			if (type.equals("nearby")) {
				clubOverview_item_distance.setVisibility(View.VISIBLE);
				clubOverview_item_distance.setText(Utils.GetLongDistance(map_y,
						map_x, Double.valueOf(clubModels.get(pos).getMap_y()),
						Double.valueOf(clubModels.get(pos).getMap_x())));
			} else {
				clubOverview_item_distance.setVisibility(View.GONE);
			}
			ImageLoader.getInstance().displayImage(Constants.BadiDownImgUrl + clubModels.get(pos).getLogo()
							+ Constants.App9block, clubOverview_item_img, options);
//			ImageLoader.getInstance().loadImage(
//					Constants.BadiDownImgUrl + clubModels.get(pos).getLogo()
//							+ Constants.App9block, options,
//					new ImageLoadingListener() {
//
//						@Override
//						public void onLoadingStarted(String arg0, View arg1) {
//							// TODO Auto-generated method stub
//
//						}
//
//						@Override
//						public void onLoadingFailed(String arg0, View arg1,
//								FailReason arg2) {
//							// TODO Auto-generated method stub
//
//						}
//
//						@Override
//						public void onLoadingComplete(String arg0, View arg1,
//								Bitmap arg2) {
//							// TODO Auto-generated method stub
//							clubOverview_item_img.setImageBitmap(Utils
//									.scaleImg(arg2, 170, 155));
//						}
//
//						@Override
//						public void onLoadingCancelled(String arg0, View arg1) {
//							// TODO Auto-generated method stub
//
//						}
//					});
		} else if (position == 0) {
			final int pos = 0;
			convertView = inflater.inflate(
					R.layout.regionhomepage_import_layout2_1, null);
			//    
			convertView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					/*if (!Utils.getUid(context).equals("")) {*/
						Intent intent = new Intent(context,
								ClubHomepageActivity.class);
						if (clubModels.get(pos).getClub_id() != null) {
							intent.putExtra("cid", clubModels.get(pos)
									.getClub_id());
						} else {
							intent.putExtra("cid", clubModels.get(pos).getId());
						}
						intent.putExtra("c_uid", clubModels.get(pos).getUid());
						intent.putExtra("is_join", clubModels.get(pos)
								.getIs_joined());
						intent.putExtra("logo", clubModels.get(pos).getLogo());
//						   
//								+ clubModels.get(pos).getId() + " "
//								+ clubModels.get(pos).getUid() + " "
//								+ clubModels.get(pos).getIs_joined() + " "
//								+ clubModels.get(pos).getLogo());
//						   ("是否已加入:"
//								+ clubModels.get(pos).getIs_joined());
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						context.startActivity(intent);
					}/* else {
						Toast.makeText(
								context,
								context.getResources()
										.getString(R.string.l_xb10), 0).show();
					}
				}*/
			});
			TextView clubOverview_item_OverAddress = (TextView) convertView
					.findViewById(R.id.clubOverview_item_OverAddress1);// 发源地
			final ImageView clubOverview_item_img = (ImageView) convertView
					.findViewById(R.id.clubOverview_item_img1);// 俱乐部背景
			TextView clubOverview_item_distance = (TextView) convertView
					.findViewById(R.id.clubOverview_item_distance);// 与我的距离

			if (type.equals("nearby")) {
				clubOverview_item_distance.setVisibility(View.VISIBLE);
				clubOverview_item_distance.setText(Utils.GetLongDistance(map_y,
						map_x, Double.valueOf(clubModels.get(pos).getMap_y()),
						Double.valueOf(clubModels.get(pos).getMap_x())));
			} else {
				clubOverview_item_distance.setVisibility(View.GONE);
			}
			clubOverview_item_OverAddress.setText(clubModels.get(pos)
					.getOriginate_address());
			convertView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
				/*	if (!Utils.getUid(context).equals("")) {*/
						Intent intent = new Intent(context,
								ClubHomepageActivity.class);
						if (clubModels.get(pos).getClub_id() != null) {
							intent.putExtra("cid", clubModels.get(pos)
									.getClub_id());
						} else {
							intent.putExtra("cid", clubModels.get(pos).getId());
						}
						intent.putExtra("c_uid", clubModels.get(pos).getUid());
						intent.putExtra("is_join", clubModels.get(pos)
								.getIs_joined());
						intent.putExtra("logo", clubModels.get(pos).getLogo());

						//    
						// + clubModels.get(pos).getClub_id() + " "
						// + clubModels.get(pos).getUid() + " "
						// + clubModels.get(pos).getIs_joined() + " "
						// + clubModels.get(pos).getLogo());
//						   ("是否已加入:"
//								+ clubModels.get(pos).getIs_joined());
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						context.startActivity(intent);
					}/* else {
						Toast.makeText(
								context,
								context.getResources()
										.getString(R.string.l_xb10), 0).show();
					}
				}*/
			});
			// ImageLoader.getInstance().displayImage(
			// Constants.BadiDownImgUrl
			// + clubModels.get(pos).getLogo(),
			// clubOverview_item_img, options);
			ImageLoader.getInstance().displayImage(Constants.BadiDownImgUrl + clubModels.get(pos).getLogo()
					+ Constants.App9block, clubOverview_item_img, options);
//			ImageLoader.getInstance().loadImage(
//					Constants.BadiDownImgUrl + clubModels.get(pos).getLogo()
//							+ Constants.App9block, options,
//					new ImageLoadingListener() {
//
//						@Override
//						public void onLoadingStarted(String arg0, View arg1) {
//							// TODO Auto-generated method stub
//
//						}
//
//						@Override
//						public void onLoadingFailed(String arg0, View arg1,
//								FailReason arg2) {
//							// TODO Auto-generated method stub
//
//						}
//
//						@Override
//						public void onLoadingComplete(String arg0, View arg1,
//								Bitmap arg2) {
//							// TODO Auto-generated method stub
//							clubOverview_item_img.setImageBitmap(Utils
//									.scaleImg(arg2, 170, 155));
//						}
//
//						@Override
//						public void onLoadingCancelled(String arg0, View arg1) {
//							// TODO Auto-generated method stub
//
//						}
//					});
			// if (!Utils.getUid(context).equals("")) {
			// Intent intent = new Intent(context, ClubHomepageActivity.class);
			// intent.putExtra("cid", clubModels.get(pos).getId());
			// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			// context.startActivity(intent);
			// } else {
			// Toast.makeText(context, "请登录", 0).show();
			// }
		} else if (position == 1) {
			final int pos = 0;
			convertView = inflater.inflate(
					R.layout.regionhomepage_import_layout2_2, null);
			//    
			convertView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					/*if (!Utils.getUid(context).equals("")) {*/
						Intent intent = new Intent(context,
								ClubHomepageActivity.class);
						if (clubModels.get(pos).getClub_id() != null) {
							intent.putExtra("cid", clubModels.get(pos)
									.getClub_id());
						} else {
							intent.putExtra("cid", clubModels.get(pos).getId());
						}
						intent.putExtra("c_uid", clubModels.get(pos).getUid());
						intent.putExtra("is_join", clubModels.get(pos)
								.getIs_joined());
						intent.putExtra("logo", clubModels.get(pos).getLogo());
//						   
//								+ clubModels.get(pos).getId() + " "
//								+ clubModels.get(pos).getUid() + " "
//								+ clubModels.get(pos).getIs_joined() + " "
//								+ clubModels.get(pos).getLogo());
//						   ("是否已加入:"
//								+ clubModels.get(pos).getIs_joined());
						intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						context.startActivity(intent);
					} /*else {
						Toast.makeText(
								context,
								context.getResources()
										.getString(R.string.l_xb10), 0).show();
					}
				}*/
			});
			TextView clubOverview_item_name = (TextView) convertView
					.findViewById(R.id.clubOverview_item_name1);// 俱乐部名称
			TextView clubOverview_item_infos = (TextView) convertView
					.findViewById(R.id.clubOverview_item_infos1);// 俱乐部介绍
			TextView clubOverview_item_addnum = (TextView) convertView
					.findViewById(R.id.clubOverview_item_addnum1);// 会员人数
			TextView clubOverview_item_price = (TextView) convertView
					.findViewById(R.id.clubOverview_item_price1);// 加入所需金额
			clubOverview_item_addnum.setText(clubModels.get(pos).getAddnum());
			clubOverview_item_infos.setText(clubModels.get(pos).getInfo());
			clubOverview_item_name.setText(clubModels.get(pos).getName());
			clubOverview_item_price.setText(clubModels.get(pos).getPrice());
			// if (!Utils.getUid(context).equals("")) {
			// Intent intent = new Intent(context, ClubHomepageActivity.class);
			// intent.putExtra("cid", clubModels.get(pos).getId());
			// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			// context.startActivity(intent);
			// } else {
			// Toast.makeText(context, "请登录", 0).show();
			// }
		}

		return convertView;
	}

	public double getMap_x() {
		return map_x;
	}

	public void setMap_x(double map_x) {
		this.map_x = map_x;
	}

	public double getMap_y() {
		return map_y;
	}

	public void setMap_y(double map_y) {
		this.map_y = map_y;
	}
}
